export default function drawAuthCode(id, code) {
  var c = document.getElementById(id);
  if (!c) {
    return;
  }
  var w = c.offsetWidth;
  var h = c.offsetHeight;
  randomStr();
  function randomStr() {
    var ctx = c.getContext('2d');
    ctx.clearRect(0, 0, w * 100, h * 100); //先清除canvas画布
    for (let i = 0; i < 4; i++) {
      var char = code[i];
      var fs = returnNum(100, 120); //字体的大小
      var deg = returnNum(-20, 30); //字体的旋转角度
      ctx.font = fs + 'px Simhei';
      ctx.textBaseline = 'top';
      ctx.fillStyle = randomColor();
      ctx.save();
      ctx.translate(i == 0 ? 30 : 80 * i + 15, 40);
      ctx.rotate((deg * Math.PI) / 180);
      ctx.fillText(char, -15 + 5, -15);
      ctx.restore();
    }
    for (let q = 0; q < 12; q++) {
      ctx.beginPath();
      ctx.lineWidth = '1';
      ctx.strokeStyle = randomColor(); // Green path
      ctx.moveTo(returnNum(0, w * 2), returnNum(60, h * 2));
      ctx.lineTo(returnNum(0, w * 2), returnNum(80, h * 3));
      ctx.stroke(); // Draw it
    }
  }
  function returnNum(min, max) {
    return parseInt(Math.random() * (max - min) + min);
  }
}
function randomColor() {
  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  return 'rgb(' + r + ',' + g + ',' + b + ')';
}
